//
//  informationNewViewController.m
//  HQJFacilitator
//
//  Created by mymac on 2016/9/30.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "informationNewViewController.h"

@interface informationNewViewController ()
@property (nonatomic,strong) UITextField *NewTextFiled;
@property (nonatomic,strong)UIView *backGroundView;
@end

@implementation informationNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
#pragma mark >>>>>主页搜索按钮
    //主页搜索按钮
    UIButton *mainAndSearchBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
    [mainAndSearchBtn setTitle:@"完成" forState:UIControlStateNormal];

    [mainAndSearchBtn addTarget:self action:@selector(mainAndSearchBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    
    mainAndSearchBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    //把右侧的两个按钮添加到rightBarButtonItem
    UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:mainAndSearchBtn];
    self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
    CGFloat textHeight ;
    if ([self.title isEqualToString:@"新地址"]) {
        self.NewTextFiled.keyboardType = UIKeyboardTypeDefault;
        self.NewTextFiled.placeholder = @"请输入您的新地址";
        textHeight = 176.f/2;
    } else {
        self.NewTextFiled.keyboardType = UIKeyboardTypeEmailAddress;

        self.NewTextFiled.placeholder = @"请输入您的新邮箱";
        textHeight = 88.f/2;
    }
    self.backGroundView.sd_layout.leftSpaceToView(self.view,0).topSpaceToView(self.view,10+64).heightIs(textHeight).widthIs(WIDTH);
    
    self.NewTextFiled.sd_layout.leftSpaceToView(self.backGroundView,10).topSpaceToView(self.backGroundView,10).heightIs(textHeight-10).widthIs(WIDTH-20);
    
    
}



-(void)mainAndSearchBtnEvent {
    HQJLog(@"点击了完成");
    NSString *URLStr;
    if ([self.title isEqualToString:@"新地址"]) {
            URLStr = [NSString stringWithFormat:@"%@memberaddaction/memberid/%@/membertype/originator/address/%@",Api_URL,[FileEngine filePathNameReadName:@"memberid" andstye:fileLoginStyle],self.NewTextFiled.text];

    } else {
         URLStr = [NSString stringWithFormat:@"%@memberaddaction/memberid/%@/membertype/originator/email/%@",Api_URL,[FileEngine filePathNameReadName:@"memberid" andstye:fileLoginStyle],self.NewTextFiled.text];
    }
    
    NSString* encodedString = [URLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    @weakify(self);
    [RequestEngine HQJFacilitatorRequestDetailsUrl:encodedString complete:^(NSDictionary *dic) {
        HQJLog(@"修改结果是;%@",dic);
//        [dic valueForKey:@"error"]
        if ([dic[@"error"]integerValue] == 0 && [dic[@"result"][@"errmsg"] isEqualToString:@"SUCCESS"]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            
            
            /**
             延迟调用
             */
            
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.0*NSEC_PER_SEC);
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                 [self_weak_.navigationController popViewControllerAnimated:YES];
            });
           
        } else {
            switch ([dic[@"error"]integerValue]) {
                case 48020:
                    [SVProgressHUD showErrorWithStatus:@"修改失败"];

                    break;
                case 40098:
                    [SVProgressHUD showErrorWithStatus:@"参数不全"];

                    break;
                case 40061:
                    [SVProgressHUD showErrorWithStatus:@"用户不存在"];

                    break;
                default:
                    break;
            }
        }
    } andError:^(NSError *error) {
        
    } ShowHUD:YES];
    
    
}
-(UIView *)backGroundView {
    if (!_backGroundView) {
        _backGroundView = [[UIView alloc]init];
        _backGroundView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_backGroundView];
    }
    
    return _backGroundView;
}

-(UITextField *)NewTextFiled {
    if (!_NewTextFiled) {
        _NewTextFiled = [[UITextField alloc]init];
       
        _NewTextFiled.backgroundColor = [UIColor whiteColor];
        _NewTextFiled.autocorrectionType = UITextAutocorrectionTypeNo;
        _NewTextFiled.contentVerticalAlignment =  UIControlContentVerticalAlignmentTop;
        [self.backGroundView addSubview:_NewTextFiled];
    }
    
    
    
    
    
    return _NewTextFiled;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
